package com.wcgksoft.controller.admin;

import com.wcgksoft.common.util.FileHandleUtil;
import com.wcgksoft.common.util.Layui;
import com.wcgksoft.entity.GreedLand.GreedLand;
import com.wcgksoft.entity.admin.Account;
import com.wcgksoft.entity.admin.Authority;
import com.wcgksoft.entity.mainten.Mainten;
import com.wcgksoft.entity.mainten.MaintenPhoto;
import com.wcgksoft.entity.mainten.MaintenType;
import com.wcgksoft.entity.system.EnterpriseProject;
import com.wcgksoft.entity.system.SysUser;
import com.wcgksoft.service.admin.AuthorityService;
import net.sf.json.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 御风
 * @version V1.0
 * @Package com.wcgksoft.controller.admin
 * @Description TODO
 * @date 2020/5/14 10:37
 * @ClassName AuthorityController
 */
@Controller
@RequestMapping("system/authority")
public class AuthorityController {

    @Autowired
    private AuthorityService authorityService;

    @ResponseBody
    @RequestMapping(value = "findAll")
    public Layui findAll(@RequestParam(value = "page",required = false)Integer page,
                         @RequestParam(value = "limit",required = false)Integer limit){
        DetachedCriteria criteria = DetachedCriteria.forClass(Authority.class);
        Account account =(Account) SecurityUtils.getSubject().getSession().getAttribute("om_user");
        List<Authority> list = authorityService.findByConditions(criteria, page, limit);
        return Layui.data(0, "success", authorityService.getTotalCount(), list);
    }

    @ResponseBody
    @RequestMapping(value = "save")
    public Layui saveOrUpdate(@RequestParam(value = "data",required = false) String data){
        JSONObject object = JSONObject.fromObject(data);
        Authority authority = null;
        if (null==object){
            return Layui.data(1, "请求格式有误", 0, null);
        }else{
            authority = (Authority) JSONObject.toBean(JSONObject.fromObject(data), Authority.class);
            authority.setCreateTime(new Date());
            authorityService.saveOrUpdate(authority);
        }
        return Layui.data(0,"操作成功",authorityService.getTotalCount(),Layui.addList(authority));

    }

    @ResponseBody
    @RequestMapping(value = "delete")
    public Layui delete(@RequestParam(value = "id",required = false) Integer id){
        try {
            Authority authority = authorityService.findById(id);
            authorityService.delete(authority);
            return Layui.data(0,"操作成功",0,null);
        }catch (Exception e){
            e.printStackTrace();
            return Layui.data(0,"删除失败",0,null);
        }

    }

}
